package server.business.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import server.business.mapper.BloodInformationMapper;
import server.business.mapper.BloodProcessingMapper;
import server.business.pojo.BloodInformation;
import server.business.pojo.BloodProcessing;
import server.business.service.BloodInformationService;
import server.business.service.BloodProcessingService;

import java.util.List;

@Service
public class BloodProcessingServiceImpl extends ServiceImpl<BloodProcessingMapper, BloodProcessing>
        implements BloodProcessingService {

    @Autowired
    private BloodProcessingMapper bloodProcessingMapper;
    @Autowired
    private BloodInformationMapper bloodInformationMapper;
    @Autowired
    private BloodInformationService bloodInformationService;


    /**
     * 对加工后的血液成分和失效时间进行修改
     * @param bloodProcessing
     */
    @Override
    public void processing(BloodProcessing bloodProcessing) {
        LambdaQueryWrapper<BloodProcessing> queryWrapper = new LambdaQueryWrapper<BloodProcessing>();
        queryWrapper.eq(BloodProcessing::getProcessingId,bloodProcessing.getProcessingId());
        BloodProcessing bloodProcessing_01 = bloodProcessingMapper.selectOne(queryWrapper);
        BloodProcessing update = new BloodProcessing();
        update.setProcessingState("202");//加工完成
        if(bloodProcessing.getPostComponent()==""){
            //血液成分未改变
            update.setPostComponent(bloodProcessing_01.getBloodComponent());
        }else {
            //成分改变
            update.setPostComponent(bloodProcessing.getPostComponent());
            //血液信息更改
            LambdaUpdateWrapper<BloodInformation> updateWrapper1 = new LambdaUpdateWrapper<>();
            updateWrapper1.eq(BloodInformation::getBloodId,bloodProcessing_01.getBloodId());
            updateWrapper1.set(BloodInformation::getBloodComponent,bloodProcessing.getPostComponent());//血液加工完成
            bloodInformationMapper.update(null,updateWrapper1);
        }
        if(bloodProcessing.getPostFailureTime()==null){
            update.setPostFailureTime(bloodProcessing_01.getFailureTime());
        }else {
            update.setPostFailureTime(bloodProcessing.getPostFailureTime());
            //血液信息更改
            LambdaUpdateWrapper<BloodInformation> updateWrapper2 = new LambdaUpdateWrapper<>();
            updateWrapper2.eq(BloodInformation::getBloodId,bloodProcessing_01.getBloodId());
            updateWrapper2.set(BloodInformation::getFailureTime,bloodProcessing.getPostFailureTime());//血液加工完成
            bloodInformationMapper.update(null,updateWrapper2);
        }
        bloodProcessingMapper.update(update,queryWrapper);
        //更改血液状态
        LambdaUpdateWrapper<BloodInformation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BloodInformation::getBloodId,bloodProcessing_01.getBloodId());
        updateWrapper.set(BloodInformation::getState,"30");//血液加工完成
        bloodInformationMapper.update(null,updateWrapper);
    }

    /**
     * 检查该血液id是否存在
     * @param id
     * @return
     */
    @Override
    public Boolean isExist(String id){
        LambdaQueryWrapper<BloodInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BloodInformation::getBloodId,id)
                .eq(BloodInformation::getState,2);
        if(bloodInformationMapper.selectList(queryWrapper).size()==0){return false;}
        else {return true;}
    }

    /**
     * 添加一条血液加工信息
     * @param bloodProcessing
     */
    @Override
    public void insert(BloodProcessing bloodProcessing) {
        bloodProcessing.setProcessingState("201");//待加工
        bloodProcessingMapper.insert(bloodProcessing);
        //更改血液状态
        LambdaUpdateWrapper<BloodInformation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BloodInformation::getBloodId,bloodProcessing.getBloodId());
        updateWrapper.set(BloodInformation::getState,"13");//血液加工
        bloodInformationMapper.update(null,updateWrapper);
    }

    /**
     * 返回查到的血液信息
     * @param id
     * @return
     */
    @Override
    public BloodInformation getBloodInformation(String id) {
        BloodInformation bloodInformation = new BloodInformation();
        bloodInformation.setBloodId(id);
        BloodInformation bloodInfo = bloodInformationService.getBloodInfo(bloodInformation);
        return bloodInfo;
    }

    /**
     * 删除一条血液加工信息
     * @param id
     */
    @Override
    public void delete(String id) {
        LambdaQueryWrapper<BloodProcessing> queryWrapper = new LambdaQueryWrapper<BloodProcessing>();
        System.out.println(id);
        queryWrapper.eq(BloodProcessing::getProcessingId,id);

        //更改血液状态
        BloodProcessing bloodProcessing = bloodProcessingMapper.selectOne(queryWrapper);
        LambdaUpdateWrapper<BloodInformation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BloodInformation::getBloodId,bloodProcessing.getBloodId());
        updateWrapper.set(BloodInformation::getState,"2");//在库
        bloodInformationMapper.update(null,updateWrapper);

        bloodProcessingMapper.delete(queryWrapper);
    }

    @Override
    public IPage<BloodProcessing> getPage(int currentPage, int pageSize, BloodProcessing bloodProcessing) {
        if(Strings.isEmpty(bloodProcessing.getProcessingId())&&Strings.isEmpty(bloodProcessing.getBloodId())
                &&Strings.isEmpty(bloodProcessing.getProcessingState())){
            bloodProcessing.setProcessingState("201");//待加工
        }
        LambdaQueryWrapper<BloodProcessing> queryWrapper = new LambdaQueryWrapper<BloodProcessing>();

        queryWrapper
                .eq(Strings.isNotBlank(bloodProcessing.getProcessingId()), BloodProcessing::getProcessingId,bloodProcessing.getProcessingId())
                .eq(Strings.isNotBlank(bloodProcessing.getBloodId()), BloodProcessing::getBloodId,bloodProcessing.getBloodId())
                .eq(Strings.isNotBlank(bloodProcessing.getProcessingState()), BloodProcessing::getProcessingState
                        ,bloodProcessing.getProcessingState());
//                .ge(bloodTransfer.getApplyTime() != null,BloodTransfer::getApplyTime,bloodTransfer.getApplyTime())
//                .le(bloodTransfer.getApplyTime() != null,BloodTransfer::getApplyTime,bloodTransfer.getApplyTime());
        IPage page = new Page(currentPage,pageSize);
        bloodProcessingMapper.selectPage(page,queryWrapper);
        return page;
    }



}
